const app = getApp()
Page({
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    Custom: app.globalData.Custom,
    TabCur: 0,
    MainCur: 0,
    VerticalNavTop: 0,
    list: [],
    load: true,
    columns: [], // 栏目
    icon: {
      name: 'cartfill',
      isShow: true
    },
    money: 0, // 奶茶总价
    money2: 0, // 奶茶配送费
    datas: {}, // 店铺信息
    shop: {
      name: 'shop',
      isShow: true
    },
    footprint: {
      name: 'footprint',
      isShow: true
    },
    settlementOfTheCar: [], // 购物车
    bool: false, // 购物袋显示的默认
    homeNmae: "",
    fun: "", // 区分外卖还是自取
  },
  onLoad(data) {
    this.setData({
      homeNmae: data.name
    })
    this.user(); // 判断数据
  },
  user() {
    let datas = [] // 用户选择的店铺的信息
    let urls = ""; // 跳转的页面
    switch (this.data.homeNmae) {
      case "maps": // 门店自取
        if (app.globalData.datas.length == 0) {
          urls = "../maps/index"
        }
        datas = app.globalData.datas
        this.setData({
          fun: 1 // 自取是1
        })
        break;
      case "tack": // 外卖
        if (app.globalData.datasTack.length == 0) {
          urls = "../take/index"
        }
        datas = app.globalData.datasTack
        this.setData({
          fun: 0 // 外卖是0
        })
        break;
      default:
        urls = "../maps/index"
    }
    this.useritem(datas, urls)
  },
  useritem(datas, urls) {
    if (datas.length != 0) { // 判断用户地址
      this.setData({
        datas: datas
      })
      this.listRun(); // 列表
      this.items(); // 数据
    } else {
      wx.reLaunch({
        url: urls
      })
    }
  },
  items() { // 数据
    this.column(); // 栏目
    this.content(); // 内容
  },
  column() { // 栏目
    let that = this;
    wx.request({
      url: 'http://localhost:8081/interfaced/commodity/list',
      success(data) {
        let rows = data.data.rows;
        for (let a = 0; a < rows.length; a++) { // 将数组columns每个值的title设为每一个栏目
          let items = "columns[" + a + "].title"
          let datas = rows[a].tradeName
          that.setData({
            [items]: datas
          })
        }
      }
    })
  },
  content() { // 内容
    let that = this;
    wx.request({
      url: 'http://localhost:8081/interfaced/goods/list',
      success(data) {
        let rows = data.data.rows
        console.log(rows);
        for (let b = 0; b < that.data.columns.length; b++) { // 每一条栏目
          let datas = []
          for (let a = 0; a < rows.length; a++) { // 每一条奶茶的内容
            if (rows[a].shopCommodity.tradeName == that.data.columns[b].title) { // 判断相同类型的奶茶
              datas.push({
                description: rows[a].description,
                image: rows[a].image,
                name: rows[a].name,
                Price: rows[a].defaultPrice,
                num: 1,
                id : rows[a].id
              })
            }
          }
          let datas2 = "columns[" + b + "].content"
          that.setData({
            [datas2]: datas
          })
        }
      }
    })
  },
  listRun() { // 列表
    wx.showLoading({
      title: '加载中...',
      mask: true
    });
    let list = [{}];
    for (let i = 0; i < 26; i++) {
      list[i] = {};
      list[i].name = String.fromCharCode(65 + i);
      list[i].id = i;
    }
    this.setData({
      list: list,
      listCur: list[0]
    })
  },
  onReady() {
    wx.hideLoading()
  },
  tabSelect(e) {
    this.setData({
      TabCur: e.currentTarget.dataset.id,
      MainCur: e.currentTarget.dataset.id,
      VerticalNavTop: (e.currentTarget.dataset.id - 1) * 50
    })
  },
  VerticalMain(e) {
    let that = this;
    let list = this.data.list;
    let tabHeight = 0;
    if (this.data.load) {
      for (let i = 0; i < list.length; i++) {
        let view = wx.createSelectorQuery().select("#main-" + list[i].id);
        view.fields({
          size: true
        }, data => {
          list[i].top = tabHeight;
          tabHeight = tabHeight + data.height;
          list[i].bottom = tabHeight;
        }).exec();
      }
      that.setData({
        load: false,
        list: list
      })
    }
    let scrollTop = e.detail.scrollTop + 20;
    for (let i = 0; i < list.length; i++) {
      if (scrollTop > list[i].top && scrollTop < list[i].bottom) {
        that.setData({
          VerticalNavTop: (list[i].id - 1) * 50,
          TabCur: list[i].id
        })
        return false
      }
    }
  },
  shoppingBag(name) { // 加入购物车
    let data = name.currentTarget.dataset.name;
    let columns = this.data.columns;
    for (let a = 0; a < columns.length; a++) {
      for (let b = 0; b < columns[a].content.length; b++) {
        if (data == columns[a].content[b].name) {
          let item = columns[a].content[b]; // 被找到的数组
          let moneys = this.data.money + item.Price // 价格
          this.addSettlementOfTheCar(item);
          this.setData({
            money: moneys,
            money2: moneys / 10
          })
          break;
        }
      }
    }
  },
  addSettlementOfTheCar(item) { // 计算购物车
    let sum = this.data.settlementOfTheCar; // 购物车
    let name = item.name; // 名字
    let image = item.image; // 照片
    let Price = item.Price; // 价格
    let num = item.num; // 默认数量
    let add; // 添加的位数
    if (sum.length != 0) {
      for (let a = 0; a < sum.length; a++) {
        if (sum[a].name == name) {
          add = "settlementOfTheCar[" + a + "]"
          Price = sum[a].Price + Price;
          num = sum[a].num + 1;
          break;
        } else {
          if (a == sum.length - 1) {
            add = "settlementOfTheCar[" + sum.length + "]"
          }
        }
      }
    } else {
      add = "settlementOfTheCar[" + sum.length + "]"
    }
    let data = {
      name: name,
      image: image,
      Price: Price,
      num: num
    }
    this.setData({
      [add]: data
    })
  },
  closeMoney() {
    var fun = this.data.fun
    app.globalData.user.settlementOfTheCar = this.data.settlementOfTheCar
    wx.reLaunch({
      url: '../set/settlement/settlement?fun=' + fun
    })
  },
  sum() {
    let bos = !this.data.bool;
    this.setData({
      bool: bos
    })
  },
  particulars(data){ // 奶茶详情
    let id = data.currentTarget.dataset.id;
    
  }
})